1504C - Balance the Bits - CodeForces Solution


brute force constructive algorithms greedy *1600

Please click on ads to support us..

Python Code:

t=int(input())
for i in range(t):
    n=int(input())
    s=input()
    a=''
    b=''
    c1=s.count("1")
    c0=s.count("0")
    if c1%2!=0 or c0%2!=0 or s[0]!="1" or s[-1]!="1" :
        print("NO")
    else:
        print("YES")
        one=0
        zer=0
        for j in range(n):
            if s[j]=="1":
                one+=1
                if one<=c1//2:
                    a+="("
                    b+="("
                else:
                    a+=")"
                    b+=")"
            else:
                zer += 1
                if zer%2!=0:
                    a+="("
                    b+=")"
                else:
                    a+=")"
                    b+="("
        print(a)
        print(b)

C++ Code:

#include <bits/stdc++.h>
#include <iostream>
using namespace std;
typedef long long ll;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vll;
typedef vector<string> vs;
typedef vector<bool> vb;
typedef vector<vb> vvb;
typedef vector<vector<string>> vvs;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<pii> vpii;
#define rep(i, a, b) for (int i = a; i < b; i++)
#define repd(i, a, b) for (int i = a; i >= b; i--)
#define F first
#define S second
#define pb push_back
#define eb emplace_back
#define yes cout << "YES \n";
#define no cout << "NO \n";
#define enter cout << "\n";
int mod = 1e9 + 7;
void solve()
{

    int n;
    cin >> n;

    string s;
    cin >> s;

    int a = 0, b = 0;
    int e = 0, f = 0;
    int r = n / 2;

    string c(n, '('), d(n, '(');
    int alt = 0;
    rep(i, 0, n)
    {
        if (s[i] == '0')
        {

            if (alt)
            {
                a++;
                d[i] = ')';
                alt = 0;
            }
            else
            {
                c[i] = ')';
                b++;

                alt = 1;
            }
        }
    }
    rep(i, 0, n)
    {
        if (s[i] == '1')
        {
            if (a < r)
            {
                a++;
            }
            else
            {
                c[i] = ')';
            }
            if (b < r)
            {
                b++;
            }
            else
            {
                d[i] = ')';
            }
            if(c[i]!=d[i]){
                no
                return;
            }
        }
    }
    a = 0;
    for (auto i : c)
    {
        if (i == '(')
        {
            a++;
        }
        else
            a--;
        if (a < 0)
        {
            no return;
        }
    }
    if (a != 0)
    {
        no return;
    }
    for (auto i : d)
    {
        if (i == '(')
        {
            a++;
        }
        else
            a--;
        if (a < 0)
        {
            no return;
        }
    }
    if (a != 0)
    {
        no return;
    }
    yes
            cout
        << c;
    enter
            cout
        << d;
    enter
}
void inc()
{
#ifndef ONLINE_JUDGE
    freopen("input.txt", "r", stdin);
    freopen("output.txt", "w", stdout);
#endif // ONLINE_JUDGE
}

int main()
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    inc();
    int t = 1;
    cin >> t;
    // creatSieveforPM();

    while (t--)
    {

        solve();
    }

    return 0;
}


Comments

Submit
0 Comments
More Questions

766A - Mahmoud and Longest Uncommon Subsequence
701B - Cells Not Under Attack
702A - Maximum Increase
1656D - K-good
1426A - Floor Number
876A - Trip For Meal
1326B - Maximums
1635C - Differential Sorting
961A - Tetris
1635B - Avoid Local Maximums
20A - BerOS file system
1637A - Sorting Parts
509A - Maximum in Table
1647C - Madoka and Childish Pranks
689B - Mike and Shortcuts
379B - New Year Present
1498A - GCD Sum
1277C - As Simple as One and Two
1301A - Three Strings
460A - Vasya and Socks
1624C - Division by Two and Permutation
1288A - Deadline
1617A - Forbidden Subsequence
914A - Perfect Squares
873D - Merge Sort
1251A - Broken Keyboard
463B - Caisa and Pylons
584A - Olesya and Rodion
799A - Carrot Cakes
1569B - Chess Tournament